import axios from 'axios'
import { ElLoading } from 'element-plus'
import { ElMessage } from 'element-plus'
//使用create创建axios实例
const loadingObj = null
const Service = axios.create({
    timeout:8000,
    baseURL:'',
    header:{
        'Content-type':"application/json;charset=utf-8"
    }
})

//请求拦截 - 对请求做统一处理
Service.interceptors.request.use(config=>{
    loadingObj = ElLoading.service({
        lock:true,
        text:'加载中...',
        background: 'rgba(0, 0, 0, 0.7)',

    })
    return config
})
//响应拦截 - 对返回值做统一处理
Service.interceptors.response.use(response=>{
    loadingObj.close()
    return response.data
},error=>{
    loadingObj.close()
    ElMessage({
        message:'服务器错误',
        type:'error',
        duration:2000
    })
})
//post请求
export const post=config=>{
    return Service({
        ...config,
        method:'post',
        data:config.data
    })
}
//get请求
export const get=config=>{
    return Service({
        ...config,
        method:'get',
        data:config.data
    })
}